//
//  MHSearchResultViewController.m
//  MHO
//
//  Created by papo on 4/12/14.
//  Copyright (c) 2014 maihoang. All rights reserved.
//

#import "MHSearchResultViewController.h"
#import "MHNavibarView.h"
#import "MHSearchViewController.h"
#import "Lib.h"

@interface MHSearchResultViewController ()

@property (strong, nonatomic) MHNavibarView* navibarView;
@property (strong, nonatomic) MHListItemViewController* listViewController;

@end

@implementation MHSearchResultViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navibarView = [MHNavibarView createWithTitle:nil leftButton:nil rightButton:nil searchButton:NO];
    [self.navibarView setActionSelectorDelegate:self left:@selector(backButtonPressed:) right:nil search:nil];
    [self.navibarView setRootViewController:self];
    [self.view addSubview:self.navibarView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)backButtonPressed:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Search Delegate 
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
    if (self.listViewController) {
        [self.listViewController.view removeFromSuperview];
        [self.listViewController removeFromParentViewController];
        self.listViewController = nil;
    }

    self.listViewController = [[MHSearchViewController alloc] initWithNibName:@"MHSearchViewController" bundle:nil];
    self.listViewController.isShowHot = NO;
    self.listViewController.listUrl = [NSString stringWithFormat:@"http://mobile.mho.vn/services/searchgame?IdSystem=1&q=%@&p=%@&s=%@&key=%@", searchBar.text, @"%d", @"%d", kServerKey];

//    self.listViewController.view.layer.borderColor = [UIColor redColor].CGColor;
//    self.listViewController.view.layer.borderWidth = 2;
//    self.listViewController 
    [self.view addSubview:self.listViewController.view];

    [self addChildViewController:self.listViewController];
    CGRect frame = self.view.frame;
    frame.origin.y += (self.navibarView.frame.size.height + searchBar.frame.size.height);
    frame.size.height -= (self.navibarView.frame.size.height + searchBar.frame.size.height);
    self.listViewController.view.frame = frame;
}

@end
